package com.shop.manager.controller;

//import com.shop.manager.entity.Content;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Content;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.ContentService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 内容增删改查
 */
@RestController
@RequestMapping("/content")
public class ContentController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private ContentService contentService;

    /**
     * @Description 添加内容信息
     * @param content 内容对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "Content 对象") Content content) {
        try {
            contentService.add(content);
            return HttpResult.ok("添加栏目内容成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加栏目内容失败!");
    }

    /**
     * @Description 批量删除内容信息
     * @param ids 内容id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids) {
        try {
            contentService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改内容信息
     * @param content 修改的内容对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "Content 对象") Content content) {
        try {
            contentService.update(content);
            return HttpResult.ok("修改栏目内容成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改栏目内容失败!");
    }

    /**
     * @Description 根据id获取内容信息
     * @param id 内容id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(contentService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param content 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) @ApiParam(value = "Content 对象") Content content) {
        try {
            //分页查询
            PageResult result = contentService.findPage(pageNum, size, content);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询栏目内容异常");
        }
    }

    /**
     * @Description 分页查询
     * @param page 当前页码
     * @param rows 当前页条数
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
        PageResult pageResult = contentService.findPage(page, rows);
        return pageResult;
    }

    /**
     * @Description 模糊查询分类信息
     * @param content 分类对象
     * @return
     */
    @RequestMapping(value = "/findByWhere", method = RequestMethod.POST)
    public HttpResult findByWhere(@RequestBody(required = false) @ApiParam(value = "Content 对象") Content content) {
        return HttpResult.ok(contentService.findByWhere(content));
    }

    /**
     * @Description 根据类目id获取类目内容类别(根据分类ID查询广告)
     * @param categoryId
     * @return
     */
    @RequestMapping("/categoryContentList")
    public HttpResult categoryContentList(@RequestParam("categoryId") Long categoryId){
        List<Content> contents = contentService.categoryContentList(categoryId);
        return HttpResult.ok(contents);
    }










}
